package com.eptok.weixin.filter;

import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.eptok.weixin.action.IndexController;

public class BusiCacheFilter implements Filter {
	private static final Logger log = Logger.getLogger(IndexController.class);
	private FilterConfig config = null;
	private HashMap expiresMap = new HashMap();

	public void init(FilterConfig filterConfig) {
		this.config = filterConfig;
		expiresMap.clear();
		Enumeration names = config.getInitParameterNames();
		while (names.hasMoreElements()) {
			try {
				String name = (String) names.nextElement();
				String value = config.getInitParameter(name);
				Integer expire = Integer.valueOf(value);
				expiresMap.put(name, expire);
			} catch (Exception ex) {
			}
		}
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;

		String uri = req.getRequestURI();
		String ext = null;
		int dot = uri.lastIndexOf(".");
		if (dot != -1) {
			ext = uri.substring(dot + 1);
		}

		setResponseHeader(res, uri, ext);
		chain.doFilter(request, response);
	}

	public void destroy() {
	}

	protected FilterConfig getFilterConfig() {
		return (config);
	}

	private void setResponseHeader(HttpServletResponse response, String uri, String ext) {
		if (ext != null && ext.length() > 0) {
			Integer expires = (Integer) expiresMap.get(ext);
			Date d = new Date();
			if (expires != null) {
				log.info(uri + ".Expires: " + expires.intValue());
				if (expires.intValue() > 0) {
					response.setHeader("Cache-Control", "max-age=" + expires.intValue()); // HTTP
					response.setDateHeader("Last-Modified", d.getTime());
					response.setDateHeader("Expires",d.getTime()+expires*1000); //Expires:过时期限值 
					response.setHeader("Pragma", "Pragma");
				} else {
					response.setHeader("Cache-Control", "no-cache");
					response.setHeader("Pragma", "no-cache"); // HTTP 1.0
					response.setDateHeader("Expires", 0);
				}
			}
		}
	}
}